package org.example;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.executiongraph.Execution;
import org.apache.flink.util.Collector;

public class WorkCountBatchDemo {
    public static void main(String[] args) throws Exception {
        // 1. 创建执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        // 2. 读取数据：从文件中读取
        DataSource<String> dataSource = env.readTextFile("input/words.txt");

        // 3. 切分、转换 (word 1)
        FlatMapOperator<String, Tuple2<String, Integer>> flatMap = dataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> collector) throws Exception {
                // 3.1 按照空格切分单词
                String[] words = value.split(" ");
                // 3.2 将单词转为 (word, 1)
                for (String word : words) {
                    Tuple2<String, Integer> tuple2 = Tuple2.of(word, 1);
                    // 3.3 使用 Collector 向下游发送数据
                    collector.collect(tuple2);
                }
            }
        });

        // 4. 按照单词分组
        UnsortedGrouping<Tuple2<String, Integer>> group = flatMap.groupBy(0);


        // 5. 分组内聚合
        AggregateOperator<Tuple2<String, Integer>> sum = group.sum(1); // 1 是位置，表示第二个元素

        // 6. 输出
        sum.print();


    }
}
